﻿using System;
using System.Windows.Forms;
using QuanLyThongTinCoSo.BaseForm;
using QuanLyThongTinCoSo.Biz;
using QuanLyThongTinCoSo.Common;

namespace QuanLyThongTinCoSo.Forms.Phong
{
    public partial class frmPhongList : FrmView
    {
        public frmPhongList()
        {
            InitializeComponent();

            grvPhong.AutoGenerateColumns = false;
            LoadData();
        }

        private void AddNew(object sender, EventArgs e)
        {
            using (var frm = new frmPhongDetail())
            {
                frm.OnLoadData += LoadData;
                frm.ShowDialog();
            }
        }

        protected override void Edit()
        {
            EditData();
        }

        private void EditData()
        {
            if (grvPhong.SelectedRows.Count > 0)
            {
                var selectedRow = grvPhong.SelectedRows[0];
                var id = Convert.ToInt32(selectedRow.Cells[0].Value.ToString());

                using (var fEdit = new frmPhongDetail(id))
                {
                    fEdit.OnLoadData += LoadData;
                    fEdit.ShowDialog();
                }
            }
            else
            {
                AlertList.Error(Constant.Messages.UpdateSelectItemError);
            }
        }

        protected override void LoadData()
        {
            var phongBiz = new PhongBiz();

            grvPhong.DataSource = null;
            grvPhong.Refresh();
            grvPhong.DataSource = phongBiz.GetAll();
        }

        protected void SelectionChanged(object sender, System.EventArgs e)
        {
        }

        private void DeleteData()
        {
            try
            {
                if (grvPhong.SelectedRows.Count != 0)
                {
                    var id = Convert.ToInt32(grvPhong.SelectedRows[0].Cells[0].Value.ToString());
                    if (DialogResult.Yes == AlertList.Question(Constant.Messages.DeleteConfirm))
                    {
                        var phongBiz = new PhongBiz();

                        var model = phongBiz.GetItemById(id);
                        phongBiz.Delete(model);
                        LoadData();
                    }
                }
                else
                {
                    AlertList.Error(Constant.Messages.DeleteSelectItemError);
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        protected override void Delete()
        {
            DeleteData();
        }

        private void ViewDetail(object sender, EventArgs e)
        {
            if (grvPhong.SelectedRows.Count > 0)
            {
                var selectedRow = grvPhong.SelectedRows[0];
                var id = Convert.ToInt32(selectedRow.Cells[0].Value.ToString());

                using (var displayForm = new FrmPhongDisplay(id))
                {
                    displayForm.ShowDialog();
                }
            }
            else
            {
                AlertList.Error(Constant.Messages.DisplaySelectItemError);
            }
        }
    }
}